package org.docshare.zhuishu;

import java.util.List;
import java.util.Map;

import org.docshare.http.DataServer;
import org.docshare.http.YangResponse;
import org.docshare.zhuishu.RefleshListView.IReflashListener;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class ConfigFragment extends Fragment implements IReflashListener,AdapterView.OnItemClickListener {
	
	private String param_id;

	public ConfigFragment(String url,String id) {
		this.url = url;
		this.param_id = id;
	}
	public ConfigFragment() {
		url = "view://test";
	}
	
    RefleshListView list;
    TextView msgtv;
    ProgressBar loadPB;
    
	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View v= inflater.inflate(R.layout.fragment_cfg, null);
		loadPB = (ProgressBar)v.findViewById(R.id.loadPB);
		list = (RefleshListView) v.findViewById(R.id.cfglist );
		msgtv = (TextView) v.findViewById(R.id.msgtv);
		
        list.setInterface(this);
        list.setOnItemClickListener(this);

        onReflash();
        return v;
	}

	public void showMsg(String s){
		msgtv.setText(s);
		msgtv.setVisibility(TextView.VISIBLE);
	}
	public void hideMsg(){
		msgtv.setVisibility(TextView.INVISIBLE);
	}
	

    private static final String TAG = "ConfigFragment";

   
    JSONObject cfgdata;
    JSONArray item_datas;


	private String url;
    private void initList2(){
    	if(cfgdata == null){
    		return;
    	}
    	
    	//设置页标题
    	String title = cfgdata.getString("title");
    	if(title!=null){
    		this.getActivity().getActionBar().setTitle(title);
    	}
    	
    	//设置list类型
    	String style = cfgdata.getString("style");
    	
        String[] from = null;
        int[] to = null; 
        int item_layout = R.layout.item_cfg_itt;
        
        if(style.equals("itt")){
        	item_layout = R.layout.item_cfg_itt;
    		from = new String[]{"img","bigt","smallt"};
    		to = new int[]{R.id.cfgimg,R.id.cfgbigt,R.id.cfgsmallt};
        }else if(style.equals("it")){
        	item_layout = R.layout.item_cfg_it;    
    		from = new String[]{"img","bigt"};
    		to = new int[]{R.id.cfgimg,R.id.cfgbigt};        	
        }else if(style.equals("tt")){
        	item_layout = R.layout.item_cfg_tt;
    		from = new String[]{"bigt","smallt"};
    		to = new int[]{R.id.cfgbigt,R.id.cfgsmallt};
        }else if(style.equals("t")){
        	item_layout = R.layout.item_cfg_t;
        	from = new String[]{"bigt"};
        	to = new int[]{R.id.cfgbigt};
        }
    	
        Log.i(TAG,"item_layout = "+item_layout);
        
    	
        item_datas = cfgdata.getJSONArray("data");
        Object o = item_datas;
        
/*        @SuppressWarnings("unchecked")
		SimpleAdapter adapter = new SimpleAdapter(this.getContext(),
        		(List<? extends Map<String, ?>>)o,
        		item_layout,
        		from,to);*/
        
        
        MyListAdapter myListAdapter = new MyListAdapter(this.getContext(),item_layout,  cfgdata);
        
        
        list.setAdapter(myListAdapter);
        
        
    }

    @Override
    public void onReflash() {
    	hideMsg();
    	loadPB.setVisibility(View.VISIBLE);
        DataServer.getIns(this.getContext()).view(url,param_id,this.getContext(),new YangResponse() {
            @Override
            public void onResult(boolean succ, Object response) {
            	if(succ){
    	            Log.i(TAG, "onResult: "+response);
    	            if(response instanceof String){
    	            	Log.e(TAG, response.toString());
    	            	return;
    	            }
    	            cfgdata = (JSONObject) response;
    	            initList2();
    	            list.reflashComplete();
                	loadPB.setVisibility(View.INVISIBLE);
                	hideMsg();
            	}else{
            		if(response == null){
            			response = "获取列表失败";
            		}
            		//Toast.makeText(ConfigFragment.this.getContext(), (String)response,Toast.LENGTH_LONG).show();
            		showMsg((String) response);

                	loadPB.setVisibility(View.INVISIBLE);
            	}
            }
        });

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        JSONObject fitem = null;
        try {
            fitem = item_datas.getJSONObject((int) id);
            //int bookid = fitem.getInteger("id");
            
            String href = fitem.getString("href");
            
            if(href!=null&& href.startsWith("view://read")){
            	Intent in =new Intent(this.getContext(),ReaderActivity.class);
            	in.putExtra("url", fitem.getString("id"));
            	in.putExtra("title", fitem.getString("bigt"));
            	startActivity(in);
            	return;
            }
            
            if(href!=null&&href.startsWith("view://")){
            	ConfigFragment cf=new ConfigFragment(href,fitem.getString("id"));
                GlobalConfig.mainAcitivity.switchToFragment(cf,true);
            }else{ //是别的Fragment
            	if(href == "fav"){
            		FavFragment ff = new FavFragment();
                    GlobalConfig.mainAcitivity.switchToFragment(ff,true);
            	}else{
            		Toast.makeText(ConfigFragment.this.getContext(), "未知的界面"+href,Toast.LENGTH_LONG).show();
            	}
            }
            
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
